package com.innoking.docsys.user.service.impl;

import com.innoking.docsys.user.service.GetPyToNplService;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * ClassName: getPyToNplServiceImpl
 * Function:  获取分词器
 * Date:      2019/8/22 9:10
 * author     yeruixaing
 * QQ:        3141684159
 * version    V1.0
 */
@Service
public class GetPyToNplServiceImpl implements GetPyToNplService {
    @Override
    public String getPyToNpl() {
        try {
            //加载python接口并获取内容
            String[] args1 = new String[]{"python", "D:\\pythonexe\\nplSnow.py"};
//            String[] args1 = new String[]{"python3", "/home/pythonexe/nplSnow.py"};
            Process pr = Runtime.getRuntime().exec(args1);
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            if(in == null){
                return "请检查文件！";
            }
            String txt = "";
            String str = "";
            while((txt = in.readLine()) != null) {
                str += txt + ",";
            }
            File file = new File("D:\\Qiju_Li");
//            File file = new File("/home/solr-8.2.0/server/solr-webapp/webapp/WEB-INF/classes");
            if (!file.exists()) {//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
            String content = "";
            StringBuilder sb = new StringBuilder();
            //读取txt文件中的路径并将读取内容写入所读取文件的路径中
            File file1 = new File("D:\\pythonexe\\javafile.txt");
//            File file1 = new File("/home/solrdic/javafile.txt");
            BufferedReader bf = new BufferedReader(new FileReader(file1));
            while (content != null) {
                content = bf.readLine();
                if (content == null) {
                    break;
                }
                sb.append(content.trim());
            }
            if(str == null || "".equals(str)){
                return "文本格式错误！";
            }
            String str1 = str.replaceAll("\\[", "");
            String str2 = str1.replaceAll("]", "");
            String str3 = str2.replaceAll("“", "");
            String str4 = str3.replaceAll("”", "");
            String str5 = str4.replaceAll("'", "")
                    .replaceAll("ufeff", "")
                    .replaceAll("\\\\", "")
                    .replaceAll("\"", "");
            String[] split = str5.split(",");
            if (split != null && !"".equals(split)) {
                FileWriter fwriter = new FileWriter(sb.toString());
                Set hashSet = new HashSet();
                for (int i = 0; i < split.length; i++) {
                    hashSet.add(split[i]);
                }
                String[] hashSetString = hashSet.toString().replaceAll("\\[", "")
                        .replaceAll("]", "").replaceAll(" ", "")
                        .split(",");
                for (int i = 0; i < hashSetString.length; i++) {
                    fwriter.write(hashSetString[i] + "\r\n");
                }
                fwriter.flush();
                fwriter.close();//一定要关闭文件
            }
            in.close();
            pr.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            return "文件创建失败";
        }
        return "创建文件完成";
    }
}
